package br.com.agenda.crud.ui;

import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;

import br.agenda.crud.db.ConexaoMySQL;
import br.com.agenda.crud.Agenda;
import br.com.agenda.crud.Contato;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;


import java.awt.BorderLayout;

public class Contatos {
	
	public static ConexaoMySQL conexao;
	private JFrame frame;
	private Agenda agenda = new Agenda();
	public List<Contato> contatos = agenda.listarContato();
	JList listaDeContatos = new JList();
	
	
	private void atualizarLista() {
		
		
		
		listaDeContatos.setListData(contatos.toArray());
	}
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					conexao = new ConexaoMySQL();
					conexao.conectar();
					
					
					Contatos window = new Contatos();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	};

	/**
	 * Create the application.
	 */
	public Contatos() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame();
		frame.setBounds(100, 100, 459, 338);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel_scroll = new JPanel();
		panel_scroll.setBounds(20, 42, 225, 207);
		panel_scroll.setAutoscrolls(true);
		frame.getContentPane().add(panel_scroll);
		panel_scroll.setLayout(new BorderLayout(0, 0));
		panel_scroll.add(listaDeContatos);
		
		listaDeContatos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		
		
		JScrollPane scrollPane = new JScrollPane(listaDeContatos);
		
		scrollPane.setAutoscrolls(true);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		panel_scroll.add(scrollPane);
		
		JButton btnC = new JButton("C");
		btnC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Contato aux = new Contato();
				agenda.createContato(aux);
				TelaDeContatos contatos = new TelaDeContatos(aux);
				contatos.frame.setVisible(true);
			}
		});
		btnC.setBounds(10, 266, 89, 23);
		frame.getContentPane().add(btnC);
		
		JButton btnR = new JButton("R");
		btnR.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				atualizarLista();
			}

			
		});
		btnR.setBounds(109, 266, 89, 23);
		frame.getContentPane().add(btnR);
		
		JButton btnU = new JButton("U");
		btnU.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listaDeContatos.getSelectedValue() == null){
					JOptionPane.showMessageDialog(null, "Selecione um contato.");
				}else{
					Contato c = (Contato)listaDeContatos.getSelectedValue();
					TelaDeContatos tela = new TelaDeContatos(c);
					tela.frame.setVisible(true);
				}
				
			}
		});
		btnU.setBounds(208, 266, 89, 23);
		frame.getContentPane().add(btnU);
		
		JButton btnD = new JButton("D");
		btnD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Contato contato = (Contato)listaDeContatos.getSelectedValue();
				agenda.deleteContato(contato);
				atualizarLista();
				
			}
		});
		btnD.setBounds(307, 266, 89, 23);
		frame.getContentPane().add(btnD);
		
		JLabel lblSejaBemVindo = new JLabel("Seja bem vindo \u00E0 agenda dos Picolinos");
		lblSejaBemVindo.setBounds(10, 11, 318, 14);
		frame.getContentPane().add(lblSejaBemVindo);
		
		
		ImageIcon icon = new ImageIcon("picolinos/imagens/picolino.jpg");   
		JLabel lblNewLabel = new JLabel(icon);  
		
		lblNewLabel.setBounds(255, 11, 178, 244);
		frame.getContentPane().add(lblNewLabel);
		
		
		atualizarLista();
		
		
		
	}
}
